/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : SimpleArrayHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 21, 2009  Raghu Sarraff	 		        Created
 */
package net.lwf.handler.math.arrays;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

public class SimpleArrayHandler extends ProcessHandler implements
		IProcessHandler {
	
	private String grade = null;
	
	public SimpleArrayHandler(String grade) {
		this.grade = grade;
	}

	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	/**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		return null;
	}

	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		DataArray dataarray = ArrayUtil.generateArray(0);
		qinfo.setQuestionObject(dataarray);
		qinfo.setAnswer(dataarray.getRows() + " x " + dataarray.getColumns());
				//Set radio answers
		if(!isMultiple) {
			qinfo.setRadioAnswerChoices(this.getRadioAnswers(dataarray));
			qinfo.setPlaceHolder("Find the array size from the following display.");
		}
		return qinfo;
	}
	
	private List<RadioData> getRadioAnswers(DataArray dataarray) {
		List<RadioData> rdata = new ArrayList<RadioData>();
		String answer = dataarray.getRows() + " x " + dataarray.getColumns();
		String display = dataarray.getRows() + " rows of " + dataarray.getColumns() + " ( " + dataarray.getRows() + " x " + dataarray.getColumns() + " )";
		rdata.add(new RadioData(answer, display));
		int required_choices = 3;
		
		List<Integer> row_choices = RandomNumberGenerator.generateIntsWithVariance(dataarray.getRows(), 3, required_choices);
		List<Integer> column_choices = RandomNumberGenerator.generateIntsWithVariance(dataarray.getColumns(), 3, required_choices);
		
		for(int i=0; i<required_choices; i++) {
			int rows = row_choices.get(i).intValue();
			int columns = column_choices.get(i).intValue();
			answer = String.valueOf(rows) + " x " + String.valueOf(columns);
			display = rows + " rows of " + columns + " ( " + rows + " x " + columns + " )"; 
			rdata.add(new RadioData(answer, display));
		}

		Collections.shuffle(rdata);
		
		return rdata;
	}
}

